package com.agmte.api.app.controller;


import com.agmte.api.app.entity.ApiGroup;
import com.agmte.api.app.entity.ApiInfo;
import com.agmte.api.app.entity.ApiUser;
import com.agmte.api.app.entity.pojo.ApiMapper;
import com.agmte.api.app.service.ApiInfoService;
import com.agmte.api.common.crud.core.CrudDb;
import com.agmte.api.common.crud.core.pojo.Page;
import com.agmte.api.common.crud.core.pojo.Pagination;
import com.agmte.api.component.response.CustomApiResult;
import com.agmte.api.config.filter.Knife4ApiScanner;
import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "接口管理")
public class CustomApiG45d4Controller {

    @Resource
    private Knife4ApiScanner scanner;

    @Autowired
    private ApiInfoService apiInfoService;

    @PostMapping("/agmte/custom/api/get/**")
    @ApiOperation(value = "访问接口")
    @ApiOperationSupport(order = 1)
    public CustomApiResult getApi(HttpServletRequest request, @RequestBody JSONObject json) throws Exception {
        String requestURI = request.getRequestURI();
        String path = requestURI.substring(requestURI.indexOf("/agmte/custom/api/get") + "/agmte/custom/api/get".length());
        JSONObject result = apiInfoService.getApi(path, json);
        return CustomApiResult.success(result);
    }

    @PostMapping("/agmte/custom/mapper/test")
    @ApiOperation(value = "mapper测试")
    @ApiOperationSupport(order = 2)
    public CustomApiResult testMapper(@RequestBody ApiMapper mapper) throws Exception {
        Object result = apiInfoService.testMapper(mapper);
        return CustomApiResult.success(result);
    }

    @GetMapping("/agmte/custom/apiInfo/params")
    @ApiOperation(value = "获取接口参数")
    @ApiOperationSupport(order = 10)
    public CustomApiResult testMapper(Long id) {
        JSONObject jsonObject = apiInfoService.generateParameterByApi(id);
        return CustomApiResult.success(jsonObject);
    }

    @GetMapping("/agmte/custom/apiInfo/allExport")
    @ApiOperation(value = "导出所有接口")
    @ApiOperationSupport(order = 11)
    public void allExport(HttpServletResponse response) throws IOException {
         apiInfoService.allExport(response);
    }

    @GetMapping("/agmte/custom/users")
    @ApiOperation(value = "获取用户列表")
    public CustomApiResult userList() {
        return CustomApiResult.success(CrudDb.list(ApiUser.class));
    }


    @PostMapping(value = "/agmte/custom/api/save")
    @ApiOperation(value = "接口-添加或修改")
    @ApiOperationSupport(order = 20)
    public CustomApiResult apiSave(@RequestBody @Valid ApiInfo apiInfo) {
        apiInfoService.save(apiInfo);
        return CustomApiResult.success(apiInfo);
    }

    @PostMapping(value = "/agmte/custom/api/import")
    @ApiOperation(value = "接口-批量导入接口")
    @ApiOperationSupport(order = 21)
    public CustomApiResult apiImport(@RequestBody @Valid List<ApiInfo> apiInfos) {
        List<String> fails = apiInfoService.batchImport(apiInfos);
        scanner.apiScanner();
        return CustomApiResult.success(fails);
    }

    @GetMapping("/agmte/custom/api/list")
    @ApiOperation(value = "接口-获取列表")
    @ApiOperationSupport(order = 21)
    public CustomApiResult apiList(Pagination pagination,
                                    String[] groupIds,
                                   String name) {
        Page<ApiInfo> list = apiInfoService.apiList(pagination,groupIds,name);
        return CustomApiResult.success(list);
    }

    @DeleteMapping("/agmte/custom/api/delete")
    @ApiOperation(value = "接口-删除")
    @ApiOperationSupport(order = 22)
    public CustomApiResult apiDelete(@RequestBody List<Long> ids) {
        int delete = CrudDb.delete(ApiInfo.class, ids);
        scanner.apiScanner();
        return CustomApiResult.success(delete);
    }

    @DeleteMapping("/agmte/custom/api/allDelete")
    @ApiOperation(value = "接口-删除所有")
    @ApiOperationSupport(order = 23)
    public CustomApiResult allApiDelete() {
        List<ApiInfo> list = CrudDb.list(ApiInfo.class);
        List<Long> ids = list.stream().map(ApiInfo::getId).collect(Collectors.toList());
        int delete = CrudDb.delete(ApiInfo.class, ids);
        scanner.apiScanner();
        return CustomApiResult.success(delete);
    }

    @PostMapping(value = "/agmte/custom/group/save")
    @ApiOperation(value = "分组-添加或修改")
    @ApiOperationSupport(order = 30)
    public CustomApiResult groupSave(@RequestBody @Valid ApiGroup apiGroup) {
        CrudDb.saveOrUpdate(apiGroup);
        scanner.apiScanner();
        return CustomApiResult.success("成功");
    }

    @GetMapping("/agmte/custom/group/list")
    @ApiOperation(value = "分组-获取列表")
    @ApiOperationSupport(order = 31)
    public CustomApiResult groupList() {
        return CustomApiResult.success(CrudDb.list(ApiGroup.class));
    }

    @DeleteMapping("/agmte/custom/group/delete")
    @ApiOperation(value = "分组-删除")
    @ApiOperationSupport(order = 32)
    public CustomApiResult groupDelete(@RequestBody List<Long> ids) {
        int delete = CrudDb.delete(ApiGroup.class, ids);
        scanner.apiScanner();
        return CustomApiResult.success(delete);
    }
}